package main

import (
	"fmt"
	"natsmicro/conf"

	"github.com/gogf/gf/frame/g"
	"github.com/nats-io/nats.go"
)

func main() {
	// ExampleJetStream()

	// // Connect to NATS
	nc, _ := nats.Connect(nats.DefaultURL)

	// Create JetStream Context
	js, _ := nc.JetStream()
	order_key := "order_key"
	js.AddStream(&nats.StreamConfig{
		Name:     conf.ConfigName,
		Subjects: []string{order_key},
	})
	// Simple Stream Publisher
	js.Publish(order_key, []byte("hello"))

	// Simple Async Ephemeral Consumer
	js.Subscribe(order_key, func(msg *nats.Msg) {
		// msg.Ack()
		fmt.Printf("Subscribe: %s\n", string(msg.Data))
	}, nats.DeliverLast())

	// Auto-ack current sequence and all below.
	js.Subscribe(order_key, func(msg *nats.Msg) {
		fmt.Printf("AckAll: %s\n", string(msg.Data))
	}, nats.AckAll(), nats.DeliverLast())

	js.QueueSubscribe(order_key, "group", func(msg *nats.Msg) {
		fmt.Printf("QueueSubscribe: %s\n", string(msg.Data))
		msg.Ack()

	}, nats.ManualAck(), nats.DeliverLast())

	js.QueueSubscribe(order_key, "group2", func(msg *nats.Msg) {
		fmt.Printf("QueueSubscribe group2 : %s\n", string(msg.Data))
		err := msg.Ack()
		fmt.Println("err", g.Export(err))
		msg.Respond(msg.Data)
	}, nats.ManualAck(), nats.DeliverLast())

	js.Publish(order_key, []byte("hello"))
	select {}
}
